from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# desirede_caps = {
#     "platformName": "Android",
#     "platformVersion": "7.1.2",
#     "deviceName": "emulator-5554",
#     "appPackage": "com.tencent.wework",
#     "appActivity": ".launch.WwMainActivity",
#     # 'fullReset': False,
#     'noReset': 'true'
# }

class BaseApp():

    def __init__(self, driver):
        self.driver = driver
        # self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desirede_caps)
        # 设置隐式等待
        self.driver.implicitly_wait(10)

    def my_find_element(self, ele, time_out=10, poll_frequency=0.5):
        """
        查找单个元素
        :param ele:
        :param time_out:
        :param poll_frequency:
        :return:
        """
        # 设置显式等待，查找元素
        self.element = WebDriverWait(self.driver, time_out, poll_frequency).until(EC.presence_of_element_located(ele))
        return self.element

    def my_find_elements(self, ele, time_out=10, poll_frequency=0.5):
        try:
            self.elements = self.driver.find_elements(*ele)
        except:
            self.elements = None

    def my_click(self, ele, time_out=10, poll_frequency=0.5):
        """
        点击元素
        :param ele:
        :param time_out:
        :param poll_frequency:
        :return:
        """
        self.my_find_element(ele, time_out, poll_frequency)
        self.element.click()

    def my_send_keys(self, ele, value, time_out=10, poll_frequency=0.5):
        """
        查找元素并输入值
        :param ele:
        :param value:
        :param time_out:
        :param poll_frequency:
        :return:
        """
        self.my_find_element(ele, time_out, poll_frequency)
        self.element.send_keys(value)

    def my_get_text(self, ele):
        self.my_find_element(ele)
        return self.element.text

    def my_quit(self):
        self.driver.quit()

    def get_index(self, app_package, app_activitty):
        self.driver.start_activity(app_package, app_activitty)

    # def back_index(self):
    #     is_index = False
    #     for i in range(5):
    #         if is_index == False:
    #             self.my_find_elements((By.ID, "com.tencent.wework:id/g68"))
    #             if self.elements:
    #                 element_text = []
    #                 for element in self.elements:
    #                     element_text.append(element.text)
    #                 element_text.sort()
    #                 tables = ['消息', '邮件', '文档', '工作台', '通讯录']
    #                 tables.sort()
    #                 if tables == element_text:
    #                     is_index = True
    #                 else:
    #                     self.driver.back()
    #             else:
    #                 self.driver.back()
    #         else:
    #             break
    #     if is_index == False:
    #         self.get_index("com.tencent.wework", ".launch.WwMainActivity")





# import time
# # 实例化对象并进入企业微信
# app = BaseApp()
#
# # 点击-通讯录按钮
# app.my_click((By.XPATH, "//*[@text='通讯录']"))
# # 进入通讯录设置
# app.my_click((By.ID, "com.tencent.wework:id/kor"))
# # 点击-添加子部门
# app.my_click((By.ID, "com.tencent.wework:id/gse"))
# # 输入部门名称
# app.my_send_keys((By.ID, "com.tencent.wework:id/cgc"), "广州8866")
# # 点击-确定
# app.my_click((By.ID, "com.tencent.wework:id/cgp"))
# # 获取提示信息
# print(app.my_find_element((By.XPATH, "//*[@text='该部门已存在']")).text)
#
# time.sleep(5)
# app.driver.quit()